package demo1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liangyuanzhang
 * Date: 2025-3-15
 * Time: 14:53
 */
public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> test = new ArrayList<>();
        test.add(1);
        test.add(2);
        test.add(3);
        test.add(4);
        test.add(5);
        System.out.println(test);

        System.out.println("=====使用迭代器listIterator 输出 拓展====");
        ListIterator<Integer> it3 = test.listIterator(test.size());//从后往前输出
        while (it3.hasPrevious()){
            System.out.print(it3.previous()+" ");
        }
        System.out.println();

        System.out.println("=====使用迭代器iterator 输出====");
        Iterator<Integer> it = test.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();

        System.out.println("=====使用迭代器listIterator 输出 ====");
        ListIterator<Integer> it2 = test.listIterator();
        while (it2.hasNext()){
            System.out.print(it2.next()+" ");
        }
        System.out.println();

/*        List<Integer> list = test.subList(1,3);
        System.out.println(list);*/

        int size = test.size();
        System.out.println("====for循环输出====");
        for (int i = 0; i < size; i++) {
            System.out.print(test.get(i)+" ");
        }
        System.out.println();
        System.out.println("=====for each循环输出====");
        for (Integer x:test) {
            System.out.print(x+" ");
        }
        System.out.println();

    }
}
